package cn.micai.base.io;

import java.io.*;
import java.util.Properties;

/**
 * 描述：How to read properties file in java
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/7 13:25
 */
public class ReadPropertiesFileJavaMain {

    public static void main(String [] args) throws IOException {

        /*System.out.println("Reading from properties file");
        System.out.println("-----------------------------");
        Properties prop = getPropertyFromSystem("src/main/resources/config.properties");
        System.out.println("host : "+prop.getProperty("host"));
        System.out.println("username : "+prop.getProperty("username"));
        System.out.println("password : "+prop.getProperty("password"));
        System.out.println("-----------------------------");*/

        /*ReadPropertiesFileJavaMain readPropertiesFileJavaMain = new ReadPropertiesFileJavaMain();
        System.out.println("Reading from properties file");
        System.out.println("-----------------------------");
        Properties properties = readPropertiesFileJavaMain.readPropertiesFileFromClasspath("/config.properties");
        System.out.println("host : " + properties.getProperty("host"));
        System.out.println("username : " + properties.getProperty("username"));
        System.out.println("password : " + properties.getProperty("password"));
        System.out.println("-----------------------------");*/

        ReadPropertiesFileJavaMain rp = new ReadPropertiesFileJavaMain();
        System.out.println("Reading file from resources folder");
        System.out.println("-----------------------------");

        rp.readFile("config.properties");

        System.out.println("-----------------------------");


    }

    // Read properties file from System
    private static Properties getPropertyFromSystem(String fileName) throws IOException {
        FileInputStream fileInputStream = null;
        Properties properties = null;
        try {
            fileInputStream = new FileInputStream(fileName);
            // create Properties class object
            properties = new Properties();
            // load properties file into it
            properties.load(fileInputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fileInputStream.close();
        }
        return properties;
    }

    // Read properties file from classpath
    public Properties readPropertiesFileFromClasspath(String fileName) throws IOException {
        InputStream inputStream = null;
        Properties properties = null;
        try {
            properties = new Properties();
            inputStream = this.getClass().getResourceAsStream(fileName);

            // create Properties class object
            if (inputStream != null) {
                // load properties file into it
                properties.load(inputStream);
            } else {
                throw new FileNotFoundException("property file '" + fileName + "' not found in the classpath");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
        return properties;
    }

    public void readFile(String fileName) {
        FileInputStream fileInputStream = null;
        try {
            // Getting ClassLoader obj
            ClassLoader classLoader = this.getClass().getClassLoader();
            // Getting resource(File) from class loader
            File configFile = new File(classLoader.getResource(fileName).getFile());

            fileInputStream = new FileInputStream(configFile);
            BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
